package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.mapper.FamilyMapper;
import com.originskyseed.platform.user.model.Family;
import com.originskyseed.platform.user.service.IFamilyService;

/**
 * 家族信息Service接口实现类（OSI_FAMILY）
 *
 * @author tangwanghui
 */
@DubboService
@Component("familyService")
public class FamilyServiceImpl implements IFamilyService {

	@Resource
	private FamilyMapper familyMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.familyMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.familyMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(Family record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.familyMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(Family record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.familyMapper.insertSelective(record);
	}

	/**
	 * 批量插入记录
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertBatchFamily(List<Family> list) throws Exception {
		return this.familyMapper.insertBatchFamily(list);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(Family record) throws Exception {
		record.setUpdateTime(new Date());
		return this.familyMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(Family record) throws Exception {
		record.setUpdateTime(new Date());
		return this.familyMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Family selectByPrimaryKey(String id) throws Exception {
		return this.familyMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询通过对象属性记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public Family selectByFimily(Family record) throws Exception {
		return this.familyMapper.selectByFimily(record);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Family> selectList(Family record) throws Exception {
		return this.familyMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Family> selectListByPage(Map<String, Object> params, BasePage<Family> page) throws Exception {
		params.put("page", page);
		List<Family> list = this.familyMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 开放平台选择家族
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<Family> selectFamilyListByPage(Map<String, Object> params, BasePage<Family> page) throws Exception {
		params.put("page", page);
		List<Family> list = this.familyMapper.selectFamilyListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询家族id是否存在
	 *
	 * @param familyIds
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<String> selectFamilyIds(List<String> familyIds) throws Exception {
		return this.familyMapper.selectFamilyIds(familyIds);
	}

	/**
	 * 根据userId查询记录
	 *
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public Family selectByUserId(String userId) throws Exception {
		Family family = this.familyMapper.selectByUserId(userId);
		if (family != null) {
			if (family.getPhone() != null) {
				String phone = family.getPhone();
				family.setPhone(phone = "-");
			}
			if (family.getBankName() != null) {
				String bankName = family.getBankName();
				family.setBankName(bankName = "-");
			}
			if (family.getBankAccount() != null) {
				String bankAccount = family.getBankAccount();
				if (StringUtils.isNotBlank(bankAccount)) {
					family.setBankAccount(bankAccount.replaceAll("(\\d{2})\\d{12}(\\d{2})", "$1****$2"));
				}
			}

		}
		return family;
	}
}
